package com.socialsoftware.Page;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.socialsoftware.R;

public class ChatInfoActivity extends AppCompatActivity {

    private String chatId;
    private String chatName;
    private Toolbar toolbar;
    private TextView tvChatName;
    private Switch switchMute, switchPin;
    private LinearLayout layoutSearchHistory, layoutSetBackground, layoutClearHistory, layoutReport;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_info);

        // Get chat details from intent
        if (getIntent() != null) {
            chatId = getIntent().getStringExtra("chat_id");
            chatName = getIntent().getStringExtra("chat_name");
        }

        // Initialize views
        initViews();

        // Set up toolbar
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }

        tvChatName.setText(chatName != null ? chatName : getString(R.string.chat_info));

        // Set up click listeners
        setupClickListeners();
    }

    private void initViews() {
        toolbar = findViewById(R.id.toolbar);
        tvChatName = findViewById(R.id.tvChatName);
        switchMute = findViewById(R.id.switchMute);
        switchPin = findViewById(R.id.switchPin);
        layoutSearchHistory = findViewById(R.id.layoutSearchHistory);
        layoutSetBackground = findViewById(R.id.layoutSetBackground);
        layoutClearHistory = findViewById(R.id.layoutClearHistory);
        layoutReport = findViewById(R.id.layoutReport);
    }

    private void setupClickListeners() {
        // Mute notifications switch
        switchMute.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // In a real app, you would save this preference to a database or shared preferences
                Toast.makeText(ChatInfoActivity.this, 
                        isChecked ? "已开启消息免打扰" : "已关闭消息免打扰", 
                        Toast.LENGTH_SHORT).show();
            }
        });

        // Pin chat switch
        switchPin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // In a real app, you would save this preference to a database or shared preferences
                Toast.makeText(ChatInfoActivity.this, 
                        isChecked ? "已置顶聊天" : "已取消置顶", 
                        Toast.LENGTH_SHORT).show();
            }
        });

        // Search chat history
        layoutSearchHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // In a real app, you would navigate to a search page
                Toast.makeText(ChatInfoActivity.this, "查找聊天记录功能开发中", Toast.LENGTH_SHORT).show();
            }
        });

        // Set chat background
        layoutSetBackground.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // In a real app, you would navigate to a background selection page
                Toast.makeText(ChatInfoActivity.this, "设置聊天背景功能开发中", Toast.LENGTH_SHORT).show();
            }
        });

        // Clear chat history
        layoutClearHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showClearHistoryConfirmation();
            }
        });

        // Report
        layoutReport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // In a real app, you would navigate to a report page
                Toast.makeText(ChatInfoActivity.this, "投诉功能开发中", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void showClearHistoryConfirmation() {
        new AlertDialog.Builder(this)
                .setTitle(getString(R.string.clear_chat_history))
                .setMessage(getString(R.string.confirm_clear_history))
                .setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // In a real app, you would clear the chat history from the database
                        Toast.makeText(ChatInfoActivity.this, "聊天记录已清空", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton(getString(R.string.cancel), null)
                .show();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 